package com.sc.smartcommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.smartcommunity.domain.Announcement;
import com.sc.smartcommunity.mapper.AnnouncementMapper;
import com.sc.smartcommunity.service.AnnouncementService;
import org.springframework.stereotype.Service;

/**
 * 公告服务实现类
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    /**
     * 分页查询公告
     */
    @Override
    public IPage<Announcement> getPage(Page<Announcement> page, String title) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();

        // 标题模糊查询
        if (title != null && !title.isEmpty()) {
            queryWrapper.like("title", title);
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc("create_time");

        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 根据ID查询公告
     */
    @Override
    public Announcement getByIdWithViewCount(Long id) {
        return baseMapper.selectById(id);
    }
}
